package com.tianquan.PaginationPlugin;/**
 * @author starsea
 * @date 2024-09-28 下午4:52
 */

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.List;



/**
 * @BelongsProject: uploaddemo
 * @BelongsPackage: com.tianquan.PaginationPlugin
 * @Author: tianquan
 * @CreateTime: 2024-09-28  16:52
 * @Description: 测试
 * @Version: 1.0
 */
// @Component
@Intercepts(
        {
                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
        }
)
public class PageUtil<T> extends AbstractPage implements Interceptor {

    private List<T> list;


    public void startPage(Integer pageNum, Integer pageSize) {
        setPage(new Page(pageNum, pageSize));
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];
        Object parameter = args[1];
        RowBounds rowBounds = (RowBounds) args[2];
        ResultHandler<?> resultHandler = (ResultHandler<?>) args[3];
        Executor executor = (Executor) invocation.getTarget();
        BoundSql boundSql = ms.getBoundSql(parameter);
        CacheKey cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
        String sql = boundSql.getSql();

        // 直接生成count语句
        MappedStatement mappedStatement = newCountMappedStatement(ms, ms.getId() + COUNT);

        // 执行count查询
        Long count = executeAutoCount(executor, mappedStatement, parameter, boundSql, rowBounds, resultHandler);

        // 获取分页参数
        Page page = getPage();
        Integer pageNum = page.getPageNum() - 1;
        Integer pageSize = page.getPageSize();

        // 拼接分页SQL
        StringBuilder sb = new StringBuilder(sql.length() + 14);
        sb.append(sql);
        sb.append(" limit ").append(pageNum * pageSize).append(", ").append(pageSize);

        String newSql = sb.toString();

        // 替换原SQL
        BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), newSql, boundSql.getParameterMappings(), parameter);

        // 执行查询,获取list数据
        List<T> list = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, newBoundSql);

        setPgeInfo( new PageInfo<>(list, count));
        return list;
    }

    public PageInfo<T> getPageInfo() {
        return super.getPageInfo();
    }


}
